package org.jsola.hr.web;

import lombok.extern.slf4j.Slf4j;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SiteContractQuotaAddDTO;
import org.jsola.hr.dto.SiteContractQuotaUpdateDTO;
import org.jsola.hr.query.SiteContractQuotaQuery;
import org.jsola.hr.service.ISiteContractQuotaService;
import org.jsola.hr.vo.SiteContractQuotaVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 租户合同配额
 *
 * @author zhr
 */
@Slf4j
@RestController("hrSiteContractQuotaController")
@RequestMapping(HrConstants.API_PATH + "/v1/siteContractQuotas")
public class SiteContractQuotaController implements ISiteContractQuotaControllerAPI {

    @Autowired
    private ISiteContractQuotaService siteContractQuotaService;

    @Override
    @PostMapping
    public Result<SiteContractQuotaVO> save(@Valid @RequestBody SiteContractQuotaAddDTO siteContractQuotaAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增租户合同配额权限校验

        return Result.success(
                siteContractQuotaService.save(siteContractQuotaAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SiteContractQuotaUpdateDTO siteContractQuotaUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改租户合同配额权限校验

        int count = siteContractQuotaService.update(siteContractQuotaUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping(value = "/{siteContractQuotaId}")
    public Result<Integer> delete(@PathVariable String siteContractQuotaId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除租户合同配额权限校验

        int count = siteContractQuotaService.deleteByIds(tokenUser, siteContractQuotaId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] siteContractQuotaIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除租户合同配额权限校验

        int count = siteContractQuotaService.deleteByIds(tokenUser, siteContractQuotaIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{siteContractQuotaId}")
    public Result<SiteContractQuotaVO> findById(@PathVariable String siteContractQuotaId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看租户合同配额权限校验

        return Result.success(
                siteContractQuotaService.selectById(siteContractQuotaId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SiteContractQuotaVO>> listAll(@Valid SiteContractQuotaQuery siteContractQuotaQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看租户合同配额权限校验

        return Result.success(
                siteContractQuotaService.select(siteContractQuotaQuery, tokenUser.getSiteId())
        );
    }

    /**
     * 查询租户合同额度
     */
    @GetMapping("selectBySite")
    public Result<SiteContractQuotaVO> selectBySite() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                siteContractQuotaService.selectBySite(tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SiteContractQuotaVO>> page(@Valid SiteContractQuotaQuery siteContractQuotaQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                siteContractQuotaService.selectPage(siteContractQuotaQuery, tokenUser.getSiteId())
        );
    }


    /**
     * 租户充值
     */
    @PutMapping("/rechargeSite")
    public Result<Integer> recharge(@Valid @RequestBody SiteContractQuotaUpdateDTO siteContractQuotaUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改租户合同配额权限校验

        int count = siteContractQuotaService.rechargeSite(siteContractQuotaUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

}

